package com.example.resourceSharingPlatform.controller;


import com.example.resourceSharingPlatform.entity.User;
import com.example.resourceSharingPlatform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 1.通过id查询用户信息 GET
     * ip:port/userId?id=xx
     *
     * 2.通过name查询用户信息 GET
     * ip:port/userName?name=xxx
     *
     * 3.查询所有用户信息 GET
     * ip:port/users
     *
     * 4.添加用户信息 POST
     * ip:port/user
     * body中填写user类的信息
     *
     * 5.根据id删除用户 DELETE
     * ip:port/user?id=xx
     *
     * 6.更新用户信息 PUT
     * ip:port/user
     * body中填写用户信息
     * */

    @GetMapping("/userId")
    User getUserById(@RequestParam(value = "id")String id){
        return userService.queryUserById(id);
    }

    @GetMapping("/userName")
    User getUserByName(@RequestParam(value = "name")String name){
        return userService.queryUserByName(name);
    }

    @GetMapping("/allUsers")
    List<User> getUsers(){
        List<User> userList = userService.queryAll();
        return userList;
    }

    @PostMapping("/user")
    User addUser(@RequestBody User user){
        return userService.insertUser(user);
    }

    @DeleteMapping("/user")
    boolean deleteUser(@RequestParam(value = "id")String id){
        return userService.deleteUserById(id);
    }

    @PutMapping("/user")
    boolean updateUser(@RequestBody User user){

        return userService.updateUser(user);
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s!", name);
    }

}
